package com.st.emps.department.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.st.emps.department.model.DepartmentModel;
import com.st.emps.department.service.DepartmentService;
import com.st.emps.util.FmtRequest;

/**
 * Servlet implementation class DepartmentServlet
 */
@WebServlet("/DepartmentServlet")
public class DepartmentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public DepartmentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		String action = request.getParameter("action");
		Object rs = "";
		switch (action) {
		case "list":
			rs = list(request);//不分页查询
			break;
		case "add":
			rs = add(request);
			break;
		case "upd":
			rs = update(request);
			break;
		case "del":
			rs = delete(request);
			break;
		case "get":
			rs = get(request);
			break;
		case "page":
			rs = page(request);//分页查询
			break;
		default:
			break;
		}
		FmtRequest.write(response.getWriter(), rs);
		
	}

	private Map<String,Object> page(HttpServletRequest request) {
		Map<String,String> fields = new HashMap<String, String>();
		fields.put("code", "code");
		fields.put("name", "name");
		DepartmentModel model = FmtRequest.parseModel(request, DepartmentModel.class,fields);
		model.setPageIndex(Integer.parseInt(request.getParameter("pageIndex")));
		model.setPageLimit(Integer.parseInt(request.getParameter("pageLimit")));
		model.setPageOn(true);
		Map<String,Object> map = new HashMap<>();
		map.put("list", service.selectList(model));
		map.put("count",service.selectCount(model));
		return map;
	}


	private String delete(HttpServletRequest request) {
		DepartmentModel model = new DepartmentModel(request.getParameter("code"));
		return service.delete(model);
	}

	private String update(HttpServletRequest request) {
		DepartmentModel model = FmtRequest.parseModel(request,DepartmentModel.class);
		return service.update(model);
	}
	
	private DepartmentModel get(HttpServletRequest request) {
		DepartmentModel model = new DepartmentModel(request.getParameter("code"));
		return service.selectModel(model);
	}

	private List<DepartmentModel> list(HttpServletRequest request) {
		DepartmentModel model = FmtRequest.parseModel(request,DepartmentModel.class);
		List<DepartmentModel> list = service.selectList(model);
		return list;
	}
	
	private String add(HttpServletRequest request) {
		DepartmentModel model = FmtRequest.parseModel(request,DepartmentModel.class);
		return service.insert(model);
	}

	private DepartmentService service = new DepartmentService();
}
